<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
class PermissiomMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$request->user()->is('admin')) {
            $route=$request->route();
            $action=$route->getAction();
            $prefix=$action["prefix"];
            $prefix=  str_replace("/", "", $prefix);
            $uri=$action["as"];
           
            $permissiom=preg_replace("/{$prefix}\./is","", $uri,1);
            $permissiom=  str_replace(".", "_", $permissiom);
            $permissiom="manage_".$permissiom;
     
            if(Auth::user()->can($permissiom)){
                
            }else{
                if($permissiom!='manage_logout'){
                    return redirect()->guest('admin/nofound');
                }
            }
          
        }
        return $next($request);
    }
}
